package util.jms;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSHelper {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void sendMessage(String message, String JNDIQueue, String ipPort){
		
		Hashtable props = new Hashtable();

		props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, ipPort);

		try {
			InitialContext ctx = new InitialContext(props);
			QueueConnectionFactory qfactory;
			qfactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
			
			Queue queue = (Queue) ctx.lookup(JNDIQueue);
			
			QueueConnection qCon = qfactory.createQueueConnection();
			QueueSession qSession = qCon.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
		
			QueueSender qSender = qSession.createSender(queue);
		
			TextMessage textMessage = qSession.createTextMessage();
			textMessage.setText(message);
		
			qSender.send(textMessage);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
}
